#include "initialdialog.h"

#include "manager.h"

#include <QComboBox>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QComboBox>
#include <QPushButton>
#include <QDir>

initialDialog::initialDialog(QWidget *parent) :
    QDialog(parent)
{
    QVBoxLayout* vLayout = new QVBoxLayout;

    m_languages = new QComboBox();
    connect(m_languages, SIGNAL(currentIndexChanged(int)), this, SLOT(loadCategories()));

    QHBoxLayout* hLayout1 = new QHBoxLayout;
    hLayout1->addWidget(new QLabel("Languages:"), 0, Qt::AlignRight);
    hLayout1->addWidget(m_languages);

    m_categories = new QComboBox();

    QHBoxLayout* hLayout2 = new QHBoxLayout;
    hLayout2->addWidget(new QLabel("Categories:"), 0, Qt::AlignRight);
    hLayout2->addWidget(m_categories);

    QHBoxLayout* hLayout3 = new QHBoxLayout;
    QPushButton* okButton = new QPushButton("OK");
    QPushButton* cancelButton = new QPushButton("Cancel");
    connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
    connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
    hLayout3->addWidget(okButton);
    hLayout3->addWidget(cancelButton);

    vLayout->addLayout(hLayout1);
    vLayout->addLayout(hLayout2);
    vLayout->addLayout(hLayout3);

    setLayout(vLayout);

    QDir dir ("./tests/languages/");
    QString l = "";
    QStringList dirs = dir.entryList(QDir::AllDirs);
    foreach (const QString& lang, dirs) {
        if (lang != "." && lang != "..") {
            if (l.isEmpty()) {
                l = lang;
            }
            m_languages->addItem(lang);
        }
    }
    if (!l.isEmpty()) {
        Manager::getManager()->m_language = l;
    }
}

void initialDialog::loadCategories()
{
    m_categories->clear();
    QString l = m_languages->currentText();
    QDir dir ("./tests/languages/" + l);
    QStringList dirs = dir.entryList(QDir::AllEntries);
    foreach (const QString& lang, dirs) {
        if (lang != "." && lang != ".." && !lang.contains("_statistics")) {
            QString s = lang;
            s = s.replace(".ini", "");
            m_categories->addItem(s);
            Manager::getManager()->m_category = s;
        }
    }
}

QString initialDialog::getLanguage() const
{
    return m_languages->currentText();
}

QString initialDialog::getCategories() const
{
    return m_categories->currentText();
}
